package com.ruoyi.web.controller.module.market;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.market.domain.Article;
import com.ruoyi.market.service.IArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 文章信息
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/market/article")
public class ArticleController extends BaseController
{
    @Autowired
    private IArticleService articleService;

    /**
     * 获取文章列表
     */
    @PreAuthorize("@ss.hasPermi('market:article:list')")
    @PostMapping("/articleList")
    public TableDataInfo list(@RequestBody Article article, HttpServletRequest request)
    {
        String shopId = request.getHeader("ShopId"); // 店铺id
        if(shopId != null && !shopId.equals("")){
            article.setShopId(shopId);
        }
        startPageByPost();
        List<Article> list = articleService.selectArticleList(article);
        return getDataTable(list);
    }

    /**
     * 检查文章是否存在 获取类型文章
     */
    @PostMapping("/checkArticleUnique")
    public AjaxResult checkArticleUnique(@RequestBody Article article, HttpServletRequest request)
    {
        String shopId = request.getHeader("ShopId"); // 店铺id
        if(shopId != null && !shopId.equals("")){
            article.setShopId(shopId);
        }
        AjaxResult ajax = AjaxResult.success();
        Article result = articleService.checkArticleUnique(article.getArticleType(), article.getShopId());
        ajax.put(AjaxResult.DATA_TAG, result);
        return ajax;
    }

    /**
     * 根据文章编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('market:article:query')")
    @GetMapping(value = { "/", "/{articleId}" })
    public AjaxResult getInfo(@PathVariable(value = "articleId", required = false) Long articleId)
    {
        AjaxResult ajax = AjaxResult.success();
        if (StringUtils.isNotNull(articleId))
        {
            Article sysArticle = articleService.selectArticleById(articleId);
            ajax.put(AjaxResult.DATA_TAG, sysArticle);
        }
        return ajax;
    }

    /**
     * 保存文章
     */
    @PreAuthorize("@ss.hasPermi('market:article:save')")
    @Log(title = "文章管理 - 保存", businessType = BusinessType.OTHER)
    @PostMapping("/articleSave")
    public AjaxResult save(@Validated @RequestBody Article article, HttpServletRequest request)
    {
        String shopId = request.getHeader("ShopId"); // 店铺id
        if(shopId != null && !shopId.equals("")){
            article.setShopId(shopId);
        }
        Article result = articleService.checkArticleUnique(article.getArticleType(), article.getShopId());
        if(result != null){
            articleService.updateArticle(article);
        }else{
            article.setCreateBy(getUsername());
            articleService.insertArticle(article);
        }
        AjaxResult ajax = AjaxResult.success();
        ajax.put(AjaxResult.DATA_TAG, article);
        return ajax;
    }

    /**
     * 新增文章
     */
    @PreAuthorize("@ss.hasPermi('market:article:add')")
    @Log(title = "文章管理 - 新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Article article, HttpServletRequest request)
    {
        String shopId = request.getHeader("ShopId"); // 店铺id
        if(shopId != null && !shopId.equals("")){
            article.setShopId(shopId);
        }
        article.setCreateBy(getUsername());
        return toAjax(articleService.insertArticle(article));
    }

    /**
     * 修改文章
     */
    @PreAuthorize("@ss.hasPermi('market:article:edit')")
    @Log(title = "文章管理 - 修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Article article)
    {
        article.setUpdateBy(getUsername());
        return toAjax(articleService.updateArticle(article));
    }

    /**
     * 删除文章
     */
    @PreAuthorize("@ss.hasPermi('market:article:remove')")
    @Log(title = "文章管理 - 删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{articleIds}")
    public AjaxResult remove(@PathVariable Long[] articleIds)
    {
        return toAjax(articleService.deleteArticleByIds(articleIds));
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('market:article:edit')")
    @Log(title = "文章管理", businessType = BusinessType.UPDATE)
    @PutMapping("/articleChangeStatus")
    public AjaxResult changeStatus(@RequestBody Article article)
    {
        article.setUpdateBy(getUsername());
        return toAjax(articleService.updateArticleStatus(article));
    }
}
